package com.shareyi.simpleserver.servlet;

import java.io.IOException;
import java.util.List;

import com.shareyi.simpleserver.core.HttpRequest;
import com.shareyi.simpleserver.core.HttpResponse;
import com.shareyi.simpleserver.handler.ServletHandler;

public class SimpleFilterChain extends FilterChain {
	
	private List<Filter> filterList;
	private boolean finished=false;
	private int idx=0;
	
	public SimpleFilterChain(){
		
	}
	
	public SimpleFilterChain(List<Filter> filters){
		this.filterList=filters;
	}


	/**
	 * 执行过滤器
	 */
	@Override
	public void doFilter(HttpRequest httpRequest, HttpResponse httpResponse)
			throws IOException, ServletException {
		 if(filterList==null||idx>=filterList.size()){
			 finished=true;
			 ServletHandler.getSingleHandler().doHandle(httpRequest.getConnection(), httpRequest, httpResponse);
			 return;
		 }
		 
		 Filter filter=filterList.get(idx);
		 filter.doFilter(httpRequest, httpResponse, this);
		 idx++;

	}

	public List<Filter> getFilterList() {
		return filterList;
	}

	public void setFilterList(List<Filter> filterList) {
		this.filterList = filterList;
	}

	public boolean isFinished() {
		return finished;
	}

	public void setFinished(boolean finished) {
		this.finished = finished;
	}
	

}
